package com.example.otherdemo.controllers;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.core.libs.JSONResult;
import com.example.core.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

@RestController
public class TestController {
  private static final BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);
  @Autowired
  ApplicationContext applicationContext;

  private static String toHexString(byte[] bytes) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
      buffer.append(String.format("%02X", bytes[i]));
    }
    return buffer.toString();
  }

  @RequestMapping("/put")
  public JSONResult put() {
    try {
      queue.put(1);
      queue.put(2);
      queue.put(3);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return new JSONResult();
  }

  @RequestMapping("/take")
  public JSONResult take() {
    Integer element = null;
    try {
      element = queue.take();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return new JSONResult().put("取出值：", element);
  }

  @RequestMapping("/print")
  public JSONResult print() {
    StringBuilder s = new StringBuilder();
    queue.forEach(o -> s.append(o).append(" - "));
    return new JSONResult().put("打印：", s.toString());
  }

  @RequestMapping("/thread")
  public JSONResult thread() {
    Thread thread = new Thread(() -> {
      while (true) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("守护线程在执行。。。");
      }
    });
    thread.setDaemon(true);
    thread.start();
    System.out.println("主线程结束");
    return new JSONResult();
  }

  @RequestMapping("/test")
  public String test() {
    return RandomUtil.randomUUID();
  }

  @RequestMapping("/tc")
  public JSONResult tc() {
    try {

      // 接口地址
      String url = "http://syx.jslife.com.cn/jsaims/as";
      // 客户号
      String cid = "880075500000002";
      // 接口版本号
      String v = "2";
      // 参数
      String p = "{\"serviceId\": \"3c.card.querydelaylistbycarno\",\"requestType\": \"DATA\",\"attributes\": {\"carNo\": \"藏-ZJSYW1\",\"parkCode\": \"0020200529\"}}";
      //签名signKey
      String signKey = "2f4ce8242b796e4adbb9bdad0794c898";
      //token
      String token = "9d258e4c9754b0f59d6c040d71d0004b";
      // 生成MD5签名
      MessageDigest md5Tool = MessageDigest.getInstance("MD5");
      byte[] md5Data = md5Tool.digest((p + signKey).getBytes("UTF-8"));
      String sn = toHexString(md5Data);

      HashMap<String, String> bodys = new HashMap<>();
      bodys.put("cid", cid);
      bodys.put("tn", token);
      bodys.put("sn", sn);
      bodys.put("v", v);
      bodys.put("p", p);

      // 发送消息和处理结果
      HttpResponse response = HttpUtils.doPost(url, bodys);
      JSONObject res = HttpUtils.parseResponseToJson(response);
      System.out.println(res.toString());
      return new JSONResult().put("mes", "执行成功");
    } catch (Exception e) {
      return new JSONResult().put("mes", "执行失败");
    }
  }
}
